package com.xxxx.comment.controller;


import com.xxxx.comment.entity.Blog;
import com.xxxx.comment.entity.dto.Result;
import com.xxxx.comment.service.IBlogService;
import com.xxxx.comment.utils.UserHolder;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private IBlogService blogService;

    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {
        blogService.saveBlog(blog);

        return Result.ok(blog.getId());
    }

    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long blogId) {
        return blogService.likeBlog(blogId);
    }

    @GetMapping("/of/me")
    public Result queryMyBlog() {
        return blogService.queryMyBlog(UserHolder.getUser().getId());
    }

    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return blogService.queryHotBlog(pageNum);
    }

    @GetMapping("/{id}")
    public Result queryBlogById(@PathVariable Long id) {

        return blogService.queryBlogById(id);
    }

    @GetMapping("/likes/{id}")
    public Result queryBlogLikes(@PathVariable("id") Long blogId) {

        return blogService.queryBlogLikes(blogId);
    }

    @GetMapping("/of/user")
    public Result queryBlogByUserId(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam("id") Long userId) {
        // 根据用户查询
        return blogService.queryMyBlog(userId);
    }

    @GetMapping("/of/follow")
    public Result queryBlogOfFollow(@RequestParam("lastId") Long max,
                                    @RequestParam(value = "offset", defaultValue = "0") Integer offset) {
        return blogService.queryBlogOfFollowf(max, offset);
    }

}
